package com.itheima.travel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.travel.constant.RedisConstant;
import com.itheima.travel.mapper.FavoriteMapper;
import com.itheima.travel.mapper.RouteMapper;
import com.itheima.travel.pojo.Route;
import com.itheima.travel.pojo.RouteExample;
import com.itheima.travel.req.AffixVo;
import com.itheima.travel.req.RouteVo;
import com.itheima.travel.req.SellerVo;
import com.itheima.travel.service.AffixService;
import com.itheima.travel.service.RouteService;
import com.itheima.travel.utils.BeanConv;
import com.itheima.travel.utils.EmptyUtil;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class RouteServiceImpl implements RouteService {

    @Autowired
    private RouteMapper routeMapper;

    @Autowired
    private FavoriteMapper favoriteMapper;

    @Autowired
    HttpSession session;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    AffixService affixService;

    @Override
    public Integer addRoute(RouteVo routeVo) {

        Object obj = session.getAttribute(session.getId());
        SellerVo sellerVo= (SellerVo) obj;
        routeVo.setSellerId(sellerVo.getId());
        return routeMapper.insert(BeanConv.toBean(routeVo, Route.class));
    }

    @Override
    public Integer updateRoute(RouteVo routeVo) {
        if (EmptyUtil.isNullOrEmpty(routeVo.getId())){
            return 0;
        }
        int flag = routeMapper.updateByPrimaryKey(BeanConv.toBean(routeVo, Route.class));
        if (flag>0){
            //删除线路缓存
            RBucket<RouteVo> bucketRouteVo = redissonClient.getBucket(
                    RedisConstant.ROUTESERVICE_FINDROUTEBYID + routeVo.getId());
            bucketRouteVo.delete();
            //删除附件缓存
            RBucket<AffixVo> bucketAffixVo = redissonClient.getBucket(
                    RedisConstant.AFFIXSERVICE_FINDAFFIXBYBUSINESSID + routeVo.getId());
            bucketAffixVo.delete();
        }
        return flag;
    }

    @Override
    public RouteVo findRouteById(RouteVo routeVo) {
        //读取缓存信息
        RBucket<RouteVo> bucket = redissonClient.getBucket(RedisConstant.ROUTESERVICE_FINDROUTEBYID + routeVo.getId());
        RouteVo routeVoCache = bucket.get();
        if (!EmptyUtil.isNullOrEmpty(routeVoCache)){
            return routeVoCache;
        }
        //如果缓存没有从数据库中查询，放入缓存
        RouteVo routeVoResult = BeanConv.toBean(routeMapper.selectByPrimaryKey(routeVo.getId()), RouteVo.class);
        if (!EmptyUtil.isNullOrEmpty(routeVoResult)){
            //查询附件信息
            List<AffixVo> affixByBusinessId = affixService.findAffixByBusinessId(AffixVo.builder().businessId(routeVo.getId()).build());
            routeVoResult.setAffixVoList(affixByBusinessId);
            bucket.set(routeVoResult,1800, TimeUnit.SECONDS);
        }
        //返回数据
        return routeVoResult;
    }

    @Override
    public PageInfo<RouteVo> findRouteByPage(RouteVo routeVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        RouteExample example = new RouteExample();
        RouteExample.Criteria criteria = example.createCriteria();

        //多种条件查询
        if (!EmptyUtil.isNullOrEmpty(routeVo.getCategoryId())){
            criteria.andCategoryIdEqualTo(routeVo.getCategoryId());
        }
        if (!EmptyUtil.isNullOrEmpty(routeVo.getRouteName())){
            criteria.andRouteNameLike("%"+routeVo.getRouteName());
        }
        if (!EmptyUtil.isNullOrEmpty(routeVo.getRouteName())){
            criteria.andRouteNameLike("%"+routeVo.getRouteName());
        }
        if (!EmptyUtil.isNullOrEmpty(routeVo.getMinPrice())){
            criteria.andPriceGreaterThan(routeVo.getMinPrice());
        }
        if (!EmptyUtil.isNullOrEmpty(routeVo.getMaxPrice())){
            criteria.andPriceLessThan(routeVo.getMaxPrice());
        }
        List<Route> routes = routeMapper.selectByExample(example);
        PageInfo<Route> pageInfo = new PageInfo<>();
        PageInfo<RouteVo> pageInfoVo=new PageInfo<>();

        //外部对象拷贝
        BeanConv.toBean(pageInfo, pageInfoVo);
        //结果集转换
        List<RouteVo> routeVoList = BeanConv.toBeanList(pageInfo.getList(), RouteVo.class);
        pageInfoVo.setList(routeVoList);
        return pageInfoVo;
    }
}
